﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Controls
{
    public class ComboBox : BaseControl, IControl
    {
        private System.Windows.Controls.ComboBox _ComboBox { get; set; }

        public ComboBox(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;
            _ComboBox = new System.Windows.Controls.ComboBox();
            this.Element = _ComboBox;


            InitSet();
            SetProperty();
            this.SetControlProperty();
        }

        string _DataType { get; set; }
        bool _IsEdit { get; set; }
        int _MaxLength { get; set; }
        bool _IsSelected { get; set; }

        private void SetProperty()
        {
            _DataType = Property.GetStringValue("DataType");
            _IsEdit = Property.GetValue<bool>("IsEdit");
            _IsSelected = Property.GetValue<bool>("IsSelected");

            _MaxLength = Property.GetValue<int>("MaxLength");

            if (Property.ContainsKey("DataSource"))
            {
                SetDateSource(Common.DictionaryListToList<ValueText>(Property["DataSource"] as List<Dictionary<string, object>>));
            }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.ComboBox c = _ComboBox;
            switch (name)
            {
                case "Name": c.Name = string.Concat("txt_", (string)value); break;
                case "Width": c.Width = Convert.ToDouble(value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "TextAlign": SetTextAlign((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Padding": c.Padding = this.GetThickness((string)value); break;
                case "Visible": SetVisible((bool)value); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "Margin": c.Margin = this.GetThickness((string)value); break;
                case "BackColor": c.Background = GetColor((string)value); break;
                case "BorderColor": c.BorderBrush = GetColor((string)value); break;
                case "BorderThickness": c.BorderThickness = this.GetThickness((string)value); break;
                case "ValueName": c.SelectedValuePath = (string)value; break;
                case "TextName": c.DisplayMemberPath = (string)value; break;
                case "ZIndex": SetZIndex((int)value); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex((int)value); break;
                case "IsEdit": c.IsEditable = (bool)value; break;
                case "DefaultValue": c.SelectedValue = value; break;
            }
        }

        public override void EventLoad()
        {
            _ComboBox.SelectionChanged += _ComboBox_SelectionChanged;
        }

        public Action<object> SelectionChanged { get; set; }

        private void _ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null) SelectionChanged(_ComboBox.SelectedItem);
        }

        public void SetDateSource(IEnumerable obj)
        {
            this._ComboBox.ItemsSource = obj;
        }

        public override object GetValue()
        {
            if (_ComboBox.SelectedValue != null) return this._ComboBox.SelectedValue;
            if (_IsEdit && !string.IsNullOrEmpty(_ComboBox.Text)) return this._ComboBox.Text;
            return null;
        }

        public override void SetValue(object value)
        {
            this._ComboBox.SelectedValue = value;

            if (this._ComboBox.SelectedValue == null && value != null) this._ComboBox.Text = value.ToString();
        }
    }

    public class ValueText
    {
        public string Value { get; set; }
        public string Text { get; set; }

        public ValueText() { }
        public ValueText(string value, string text)
        {
            Value = value;
            Text = text;
        }
    }
}
